package com.hanxing.kxcharge.module.operations.service.equipmentconnector;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.hanxing.kxcharge.module.operations.controller.admin.equipmentconnector.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.equipmentconnector.EquipmentConnectorDO;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;

import com.hanxing.kxcharge.module.operations.dal.mysql.equipmentconnector.EquipmentConnectorMapper;

import static com.hanxing.kxcharge.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hanxing.kxcharge.module.operations.enums.ErrorCodeConstants.*;

/**
 * 电枪管理 Service 实现类
 *
 * @author 航兴充电
 */
@Service
@Validated
public class EquipmentConnectorServiceImpl implements EquipmentConnectorService {

    @Resource
    private EquipmentConnectorMapper equipmentConnectorMapper;

    @Override
    public Long createEquipmentConnector(EquipmentConnectorSaveReqVO createReqVO) {
        // 插入
        EquipmentConnectorDO equipmentConnector = BeanUtils.toBean(createReqVO, EquipmentConnectorDO.class);
        equipmentConnectorMapper.insert(equipmentConnector);
        // 返回
        return equipmentConnector.getId();
    }

    @Override
    public void updateEquipmentConnector(EquipmentConnectorSaveReqVO updateReqVO) {
        // 校验存在
        validateEquipmentConnectorExists(updateReqVO.getId());
        // 更新
        EquipmentConnectorDO updateObj = BeanUtils.toBean(updateReqVO, EquipmentConnectorDO.class);
        equipmentConnectorMapper.updateById(updateObj);
    }

    @Override
    public void deleteEquipmentConnector(Long id) {
        // 校验存在
        validateEquipmentConnectorExists(id);
        // 删除
        equipmentConnectorMapper.deleteById(id);
    }

    private void validateEquipmentConnectorExists(Long id) {
        if (equipmentConnectorMapper.selectById(id) == null) {
            throw exception(EQUIPMENT_CONNECTOR_NOT_EXISTS);
        }
    }

    @Override
    public EquipmentConnectorDO getEquipmentConnector(Long id) {
        return equipmentConnectorMapper.selectById(id);
    }

    @Override
    public PageResult<EquipmentConnectorDO> getEquipmentConnectorPage(EquipmentConnectorPageReqVO pageReqVO) {
        return equipmentConnectorMapper.selectPage(pageReqVO);
    }

}